package com.supor.aiot.mine.ui.adapter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.android.baseconfig.common.callback.OnItemClickListener;
import com.android.baseconfig.recyclerview.groupadapter.adapter.GroupedRecyclerViewAdapter;
import com.android.baseconfig.recyclerview.groupadapter.holder.BaseViewHolder;
import com.supor.aiot.common.app.AppAgent;
import com.supor.aiot.common.entity.ProductCategory;
import com.supor.aiot.common.entity.ProductType;
import com.supor.aiot.mine.R;

import java.util.ArrayList;
import java.util.List;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.res.ResourcesCompat;

public class ProductAdapter extends GroupedRecyclerViewAdapter {
    private Context context;

    private List<ProductCategory> mList;
    private OnItemClickListener itemClickListener;

    public ProductAdapter(Context context, List<ProductCategory> list) {
        super(context);
        mList = list;
    }

    public void setList(List<ProductCategory> list) {
        this.mList = list;
        notifyDataChanged();
    }

    @Override
    public int getGroupCount() {
        return mList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        //如果当前组收起，就直接返回0，否则才返回子项数。这是实现列表展开和收起的关键。
        if (!isExpand(groupPosition)) {
            return 0;
        }
        ArrayList<ProductType> children = (ArrayList<ProductType>) mList.get(groupPosition).productTypes;
        return children == null ? 0 : children.size();
    }

    @Override
    public boolean hasHeader(int groupPosition) {
        return true;
    }

    @Override
    public boolean hasFooter(int groupPosition) {
        return false;
    }

    @Override
    public int getHeaderLayout(int viewType) {
        return R.layout.item_product_group;
    }

    @Override
    public int getFooterLayout(int viewType) {
        return 0;
    }

    @Override
    public int getChildLayout(int viewType) {
        return R.layout.item_product_child;
    }

    @Override
    public void onBindHeaderViewHolder(BaseViewHolder holder, int groupPosition) {
        ProductCategory entity = mList.get(groupPosition);
        holder.setText(R.id.group_item_name, entity.categoryName);
        TextView ivState = holder.get(R.id.group_item_indicator);
        if(entity.isExpand){
            ivState.setText(holder.itemView.getResources().getString(R.string.pack_up));
            ivState.setCompoundDrawablesWithIntrinsicBounds(null, null,
                    ResourcesCompat.getDrawable(holder.itemView.getResources(),
                            R.mipmap.ic_black_up, null),
                    null);
        } else {
            ivState.setText(holder.itemView.getResources().getString(R.string.expand));
            ivState.setCompoundDrawablesWithIntrinsicBounds(null, null,
                    ResourcesCompat.getDrawable(holder.itemView.getResources(),
                            R.mipmap.ic_black_down, null),
                    null);
        }
        ivState.setOnClickListener(new ViewClick(itemClickListener, groupPosition));
    }

    @Override
    public void onBindFooterViewHolder(BaseViewHolder holder, int groupPosition) {
    }

    @Override
    public void onBindChildViewHolder(BaseViewHolder holder, int groupPosition, int childPosition) {
        if (AppAgent.getScreenWidth() > 0) {
            int width =
                    (AppAgent.getScreenWidth() - mContext.getResources().getDimensionPixelSize(R.dimen.dp_80)) / 3;
            ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(width, width);
            holder.itemView.findViewById(R.id.iv_photo).setLayoutParams(params);
        }
        ProductType entity = mList.get(groupPosition).productTypes.get(childPosition);
        holder.setText(R.id.child_item_name, entity.modelTypeName);
        holder.setImageResource(R.id.iv_icon, entity.likeCount);
        // if (Constants.Type.TYPE_CHEF_MACHINE.equalsIgnoreCase(entity.modelTypeId)) {
        // } else if (Constants.Type.TYPE_OVEN.equalsIgnoreCase(entity.modelTypeId)) {
        //     holder.setImageResource(R.id.iv_icon, R.mipmap.ic_device_oven);
        // }
        holder.itemView.findViewById(R.id.iv_photo).setSelected(entity.selected);
        holder.itemView.findViewById(R.id.child_item_name).setSelected(entity.selected);
    }

    @Override
    public void onChildItemSelect(int groupPosition, int childPosition) {
        for (int i = 0; i < mList.size(); i++) {
            ProductCategory productGroup = mList.get(i);
            List<ProductType> children = productGroup.productTypes;
            for (int i1 = 0; i1 < children.size(); i1++) {
                ProductType child = children.get(i1);
                if (child.selected) {
                    child.selected = false;
                    notifyChildChanged(i, i1);
                }
            }
        }

        ProductType entity = mList.get(groupPosition).productTypes.get(childPosition);
        entity.selected = true;
        notifyChildChanged(groupPosition, childPosition);
    }

    /**
     * 判断当前组是否展开
     *
     * @param groupPosition
     * @return
     */
    public boolean isExpand(int groupPosition) {
        ProductCategory entity = mList.get(groupPosition);
        return entity.isExpand;
    }

    /**
     * 展开一个组
     *
     * @param groupPosition
     */
    public void expandGroup(int groupPosition) {
        expandGroup(groupPosition, false);
    }

    /**
     * 展开一个组
     *
     * @param groupPosition
     * @param animate
     */
    public void expandGroup(int groupPosition, boolean animate) {
        ProductCategory entity = mList.get(groupPosition);
        entity.isExpand = true;
        if (animate) {
            notifyChildrenInserted(groupPosition);
        } else {
            notifyDataChanged();
        }
    }

    /**
     * 收起一个组
     *
     * @param groupPosition
     */
    public void collapseGroup(int groupPosition) {
        collapseGroup(groupPosition, false);
    }

    /**
     * 收起一个组
     *
     * @param groupPosition
     * @param animate
     */
    public void collapseGroup(int groupPosition, boolean animate) {
        ProductCategory entity = mList.get(groupPosition);
        entity.isExpand = false;
        if (animate) {
            notifyChildrenRemoved(groupPosition);
        } else {
            notifyDataChanged();
        }
    }

    public class ViewClick implements View.OnClickListener {
        private int position;
        private OnItemClickListener itemClickListener;

        public ViewClick(OnItemClickListener itemClickListener, int position) {
            this.position = position;
            this.itemClickListener = itemClickListener;
        }

        @Override
        public void onClick(View v) {
            itemClickListener.onItemClick(v, position);
        }
    }
    public void setViewClick(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }
}
